package cn.ecnu.user.service.impl;

import cn.ecnu.common.Constant;
import cn.ecnu.exception.RRXException;
import cn.ecnu.user.mapper.UserMapper;
import cn.ecnu.user.pojo.User;
import cn.ecnu.user.service.UserService;
import cn.ecnu.user.util.TokenSolver;
import cn.ecnu.user.vo.*;
import cn.ecnu.vo.user.UserPermission;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {


    @Resource
    private UserMapper userMapper;

    @Autowired
    private TokenSolver tokenSolver;

    @Override
    public UserInfoVo loginWithPassword(UsernamePasswordVo vo) {
        /**
         * 1. 获取user判断是否为空
         * 2. 对比密码
         * 3. 生成用户token存到redis
         */
        User user = userMapper.getUserByUsername(vo.getUsername());
        if (user == null) throw new RRXException(Constant.NO_USER.getMsg(), Constant.NO_USER.getCode());
        if (!StringUtils.equals(user.getPassword(), vo.getPassword()))
            throw new RRXException(Constant.LOGIN_ERROR.getMsg(), Constant.LOGIN_ERROR.getCode());


        String token = "";
        try {
            token = tokenSolver.sign(new UserPermission(user.getUserId(), user.getRole()));
        } catch (Exception e) {
            e.printStackTrace();
        }

        UserInfoVo userInfoVo = new UserInfoVo();
        BeanUtils.copyProperties(user, userInfoVo);
        userInfoVo.setToken(token);
        return userInfoVo;
    }

    @Override
    public UserInfoVo loginWithEmail(EmailPasswordVo vo) {
        /**
         *  逻辑同上
         */
        User user = userMapper.getUserByEmail(vo.getEmail());
        if (user == null) throw new RRXException(Constant.NO_USER.getMsg(), Constant.NO_USER.getCode());
        if (!StringUtils.equals(user.getPassword(), vo.getPassword()))
            throw new RRXException(Constant.LOGIN_ERROR.getMsg(), Constant.LOGIN_ERROR.getCode());


        String token = "";
        try {
            token = tokenSolver.sign(new UserPermission(user.getUserId(), user.getRole()));
        } catch (Exception e) {
            e.printStackTrace();
        }

        UserInfoVo userInfoVo = new UserInfoVo();
        BeanUtils.copyProperties(user, userInfoVo);
        userInfoVo.setToken(token);
        return userInfoVo;
    }

    @Override
    public int registerWithUser(RegisterWithUsernameVo vo) {
        /** 注册必须同时输入username and email
         *
         */
        User user = userMapper.getUserByUsernameOrEmail(vo);
        if (user != null) throw new RRXException(Constant.USER_EXISTS.getMsg(), Constant.USER_EXISTS.getCode());

        // 注册这个用户
        User userToStore = new User();
        BeanUtils.copyProperties(vo, userToStore);
        userToStore.setRole("user");
        return userMapper.addUser(userToStore);
    }

    @Override
    public int deleteUserById(Integer uid) {
        return userMapper.deleteUserById(uid);
    }

    @Override
    public int updateUserById(UserInfoVo vo) {
        return userMapper.updateUserVoById(vo);
    }

    @Override
    public AdminAllUser getAllUsers() {
        AdminAllUser vo = new AdminAllUser();
        List<AdminUserVo> users = userMapper.getAdminUserVoAll();
        vo.setUsers(users);
        return vo;
    }
}
